﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MDS
{
    abstract public class EnumBase<T>
    {
        protected T _value;

        public EnumBase(T value)
        {
            this._value = value;
        }

        public static implicit operator T(EnumBase<T> value)
        {
            if (value == null)
                return default(T);
            else
                return value._value;
        }
       
       
        public override bool Equals(object obj)
        {
            if (obj == null)
                return false;

            if (System.Object.ReferenceEquals(this, obj))
                return true;
         
            var b = obj as EnumBase<T>;
            if ((object)b != null)
                return (object)this._value == (object)b._value || this._value.Equals(b._value);

            return (object)this._value == obj || this._value.Equals(obj);
        }

        /// <summary>
        /// 用于相同类型字段（b可能为null）进行等于比较
        /// </summary>
        /// <param name="a"></param>
        /// <param name="b"></param>
        /// <returns></returns>
        public static bool operator ==(EnumBase<T> a, EnumBase<T> b)
        {
            if (System.Object.ReferenceEquals(a, b))
                return true;

            if (((object)a == null) || ((object)b == null))
                return false;

            return (object)a._value == (object)b._value || a._value.Equals(b._value);
        }

        /// <summary>
        /// 用于相同类型字段（b可能为null）进行不等于比较
        /// </summary>
        /// <param name="a"></param>
        /// <param name="b"></param>
        /// <returns></returns>
        public static bool operator !=(EnumBase<T> a, EnumBase<T> b)
        {
            return !(a == b);
        }

        
        public override int GetHashCode()
        {
            return base.GetHashCode();
        }

        public override string ToString()
        {
            return _value.ToString();
        }
    }
}
